package org.his.controller;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.his.entity.Drug;
import org.his.service.DrugService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/drug")
public class DrugController {
	@Autowired
	private DrugService drugService;
	
	@RequestMapping("/queryalldrugs")
	@ResponseBody
	public Collection<Drug> queryalldrugs() {
		List<Drug> rsDrugs = drugService.queryAllDrugs();
		return rsDrugs;
	}
	//实现基本查询功能（分页功能未实现）
	@RequestMapping("/querybyform")
	@ResponseBody
	public Collection<Drug> querybyform(Drug qDrug){
		
		List<Drug> qDrugs = drugService.queryByFrom(qDrug);
		return qDrugs;
	}
	//增加药品
	@RequestMapping(value = "/adddrug",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addDrug(Drug addDrug) {
		int count = drugService.addDrug(addDrug);
		if (count > 0) {
			return "{\"success\":\"true\",\"message\":\"药品增加成功\"}";
		}
		return "{\"success\":\"flase\",\"message\":\"药品增加失败\"}";
	}
	//删除药品（批量删除未实现————思路使用数字存储修改行号循环删除）
	@RequestMapping(value = "/deletedrug",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String deleteDrug(@RequestParam("drugno") String drugno) {
		int count = drugService.deleteDrug(drugno);
		if (count > 0) {
			return "{\"success\":\"true\",\"message\":\"药品删除成功\"}";
		}
		return "{\"success\":\"flase\",\"message\":\"药品删除失败\"}";
	}
	//修改药品
	@RequestMapping(value = "/updatedrug",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String updateDrug(Drug uDrug) {
		int count = drugService.updateDrug(uDrug);
		if (count > 0) {
			return "{\"success\":\"true\",\"message\":\"药品修改成功\"}";
		}
		return "{\"success\":\"flase\",\"message\":\"药品修改失败\"}";
	}
	//为下拉框提供参数
	@RequestMapping("/list")
	@ResponseBody
	public Collection<Drug> queryList(@RequestParam("q")String q,@RequestParam("field")String field) {
		List<Drug> listDrugs = drugService.queryList(q);
		return listDrugs;
	}
	
	
	
}
